package com.example.warehouse.sys.dao.persist.respository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.warehouse.common.pojo.vo.PageData;
import com.example.warehouse.common.util.PageInfoToPageDataConverter;
import com.example.warehouse.sys.dao.persist.mapper.UserMapper;
import com.example.warehouse.sys.dao.persist.respository.IUserRepository;
import com.example.warehouse.sys.pojo.entity.SysUser;
import com.example.warehouse.sys.pojo.vo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class UserRepositoryImpl implements IUserRepository {
    @Autowired
    private UserMapper userMapper;

    public UserRepositoryImpl() {
        log.info("创建存储用户对象: UserRepositoryImpl");
    }

    //增
    @Override
    public int insert(SysUser user) {
        log.debug("开始向用户表中插入数据, 参数: {}",user);
        return userMapper.insert(user);
    }

    //删
    @Override
    public int deleteById(Long id) {
        log.debug("开始根据id删除用户数据, 参数: {}",id);
        return userMapper.deleteById(id);
    }

    @Override
    public int deleteByRoleId(Long roleId) {
        log.debug("开始根据role_id删除用户角色数据, 参数: {}",roleId);
        return userMapper.deleteByRoleId(roleId);
    }


    //改
    @Override
    public int updateById(SysUser user) {
        log.debug("开始根据id修改用户数据, 参数: {}",user);
        return userMapper.updateById(user);
    }

    @Override
    public List<UserCustomerStandardVO> queryByKeyWord(String deptName, String userName, String address) {
        log.debug("开始模糊查询, 参数:{},{},{}",deptName,userName,address);
        return userMapper.queryByKeyWord(deptName,userName,address);
    }

    @Override
    public Long selectByRoleId(Long id) {
        return userMapper.selectRoleId(id);
    }

    @Override
    public String selectNicknameById(Long id) {
        return userMapper.selectNicknameById(id);
    }

    @Override
    public UserStandardVO getStandardById(Long id) {
        log.debug("开始根据id查询用户数据, 参数: {}",id);
        return userMapper.getStandardById(id);
    }

    @Override
    public UserPersonalVO getPersonalById(Long id) {
        log.debug("开始根据id查询用户数据, 参数: {}",id);

        return userMapper.getPersonalById(id);
    }

    @Override
    public List<SysUser> selectAll() {
        List<SysUser> list = userMapper.selectList(null);
        return list;
    }


    //查
    @Override
    public int countByName(String name) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        return userMapper.selectCount(wrapper);
    }

    @Override
    public int countById(Long id) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        return userMapper.selectCount(wrapper);
    }


    //查
    @Override
    public UserLoginInfoVO getLoginInfoByUsername(String name) {
        log.debug("开始执行【根据用户名查询用户登录信息】的数据访问，参数：{}", name);
        return userMapper.getLoginInfoByUsername(name);
    }

    @Override
    public PageData<SysUserListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询用户列表】的业务，页码{},每页记录数{}",pageNum,pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<SysUserListItemVO> list = userMapper.list();
        PageInfo<SysUserListItemVO> pageInfo = new PageInfo<>(list);
        PageData<SysUserListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
